package com.twu.biblioteca;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by neilbo on 2/01/2014.
 */
public class BibliotecaApp {
    private Scanner input;

    public static void main (String[] args) {
        BibliotecaApp app = new BibliotecaApp(new PrintWriter(System.out, true), new Scanner(System.in) );
        app.run();
    }
    private PrintWriter printWriter;
    private Library lib;

    BibliotecaApp(PrintWriter printWriter, Scanner input) {
        this.input = input;
        lib = new Library();
        this.printWriter = printWriter;
    }


    public void showOptions() {
        printWriter.println("These are your menu options");
        printWriter.println("[1] List Books");
        printWriter.println("[2] Checkout A Book");
        printWriter.println("[3] Return A Book");
        printWriter.println("[4] Exit Biblioteca");
        printWriter.println("[5] List Of Movies");
        printWriter.println("[6] Checkout A Movie");
        printWriter.println("[7] View My Details");


    }

    public void showWelcomeMessage() {
        printWriter.println("Welcome to Biblioteca");

    }

    public void showAvailableBooks() {
        printWriter.println("List Of Books");
        for (Book book : lib.getAvailableBooks()) {
            printWriter.println(book.getTitle());
        };
    }

    public void showCheckoutBook() {
        printWriter.println("Checkout A Book");
        List<Book> books = lib.getAvailableBooks();
        int displayIndex = 0;
        int bookSize = books.size();
        String userInput = "First Book";//input.nextLine();
        for (int i = 0; i < bookSize; i++) {
            if (input.hasNext()){
                if(userInput.equals(books.get(i).getTitle())){
                    printWriter.println("Success, you have checkout " + userInput );
                } else {
                    showErrorMessage();
                }
            }
        }


    }
    public void showReturnBook() {
        printWriter.println("Return A Book");
        List<Book> books = lib.getAvailableBooks();
        int displayIndex = 0;
        int bookSize = books.size();
        for (int i = 0; i < bookSize; i++) {
            printWriter.println((displayIndex++) + ". " + books.get(i).getTitle());
        }
        printWriter.println("Please enter the INDEX NUMBER of the Book you wish to Return");
        if (input.hasNext()){
            String user = input.nextLine();
            if ("0".equals(user))
            {
                printWriter.println("Thank you for returning First Book");
            }
            else if ("1".equals(user))
            {
                printWriter.println("Thank you for returning Second Book");
            }else if ("2".equals(user))
            {
                printWriter.println("Thank you for returning Third Book");
            }else {
                showErrorMessage();
            }
        }//if (input.hasNext)

    }
    public void run() {
        showWelcomeMessage();
        //while (true){
        showOptions();
            if (input.hasNext()){
                String user = input.nextLine();
                if ("1".equals(user))
                {
                    showAvailableBooks();
                }
                else if ("2".equals(user))
                {
                    showCheckoutBook();
                }
                else if ("3".equals(user))
                {
                    showReturnBook();
                }
                else if ("4".equals(user)){
                    showExitMessage();
                    //System.exit(0);
                }
                else if ("5".equals(user)){
                    showAvailableMovies();
                }
                else if ("6".equals(user)){
                    showCheckoutMovie();
                }
                else if ("7".equals(user)){
                    showViewMyDetails();
                }
                else {
                    showErrorMessage();
                }
            } // if(input.hasNext())
       // } // while
    }// run
    public void showErrorMessage() {
        printWriter.println("This is not a valid option, Please see a librarian");
    }

    public void showExitMessage() {
        printWriter.println("Exit Biblioteca");

    }

    public void showAvailableMovies() {
       printWriter.println("List Of Movies");
        for (Movie movie : lib.getAvailableMovies()) {
            printWriter.println(movie.getInfo());
        };
    }

    public void showCheckoutMovie() {
        printWriter.println("Checkout A Movie");
        List<Movie> movieList = lib.getAvailableMovies();
        int displayIndex = 0;
        int movieSize = movieList.size();
        for (int i = 0; i < movieSize; i++) {
            printWriter.println((displayIndex++) + ". " + movieList.get(i).getInfo());
        }
        printWriter.println("Please enter the INDEX NUMBER of the Movie you wish to Checkout");
        if (input.hasNext()){
            String user = input.nextLine();
            if ("0".equals(user))
            {
                printWriter.println("Success, enjoy watching " + movieList.get(0).getTitle());
            }
            else if ("1".equals(user))
            {
                printWriter.println("Success, enjoy watching " + movieList.get(1).getTitle());
            }
            else if ("2".equals(user))
            {
                printWriter.println("Success, enjoy watching " + movieList.get(2).getTitle());
            }
            else {
                showErrorMessage();
            }

        }// If (input.hasNext())
    }//showCheckoutMovie

    public void showViewMyDetails() {
        printWriter.println("You must log in to continue");

    }




}
